// pages/orderCenter/addv2.js
import commonUtils from '../../utils/util.js'
import requestUtil from '../../baseRequest.js'
import requestUrls from '../../requestUrl.js'
import { $Message } from '../../components/base/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderCenter: {},
    orderTruckImageList: [],
    showLoading: false,
    publishStr: '发布',
  },
  getFromAddress() {
    wx.setStorageSync('orderCenter', this.data.orderCenter);
    wx.navigateTo({
      url: '/pages/truckCenter/fromAddress?orderType=orderCenter&addressType=FA',
    })
  },
  getReceiveAddress() {
    wx.setStorageSync('orderCenter', this.data.orderCenter);
    wx.navigateTo({
      url: '/pages/truckCenter/fromAddress?orderType=orderCenter&addressType=TA',
    })
  },
  getGoodsInfo() {
    wx.setStorageSync('orderCenter', this.data.orderCenter);
    wx.navigateTo({
      url: '/pages/orderCenter/goodsInfo',
    })

  },
  getTypeLength() {
    wx.setStorageSync('orderCenter', this.data.orderCenter);
    wx.navigateTo({
      url: '/pages/truckCenter/typeLength?orderType=orderCenter',
    })
  },
  getComment() {
    wx.setStorageSync('orderCenter', this.data.orderCenter);
    wx.navigateTo({
      url: '/pages/orderCenter/comments',
    })
  },
  getBeginTime() {
    this.setData({
      showTimeSelect: true,
    })
  },
  onFromCancel() {
    this.setData({
      showTimeSelect: false,
    })
  },
  onFromConfirm(event) {
    const { beginDate, endDate } = event.detail;
    this.setData({
      orderCenter: {
        ...this.data.orderCenter,
        loadingBeginTime: commonUtils.formatYMDH(new Date(beginDate)),
        loadingEndTime: commonUtils.formatYMDH(new Date(endDate)),
        freeTime: commonUtils.formatDate(new Date(beginDate), 'MM-dd hh') + "点--" + commonUtils.formatDate(new Date(endDate), 'MM-dd hh') + "点",
      },
    });
    this.onFromCancel();
  },
  removeComment() {
    wx.showModal({
      title: '温馨提示',
      content: '删除备注？',
      showCancel: true,
      success: res => {
        if (res.confirm) {
          this.setData({
            comments: null
          })
        }
      }
    })
  },
  handleChangeFee(event) {
    const transportFee = event.detail.value;
    this.setData({
      orderCenter: {
        ...this.data.orderCenter,
        transportFee,
      }
    })
  },
  publishOrder() {
    const { orderFromAddress, orderToAddress, goodsInfo, typeLength, comments, orderCenter, addOrderCenter = {} } = this.data;
    let orderTruckImageList = [];
    if (comments.goodsImgList && comments.goodsImgList.length > 0) {
      orderTruckImageList = comments.goodsImgList;
    }
    let formData = {
      openId: wx.getStorageSync('openid'),
      criteria: {
        truckCenterId: addOrderCenter.truckCenterId,
        orderCenter: {
          fromProvinceName: orderFromAddress.fromProvinceName,
          fromCityName: orderFromAddress.fromCityName,
          fromCountyName: orderFromAddress.fromCountyName,
          fromAddressDetail: orderFromAddress.fromAddressDetail,

          toProvinceName: orderToAddress.fromProvinceName,
          toCityName: orderToAddress.fromCityName,
          toCountyName: orderToAddress.fromCountyName,
          toAddressDetail: orderToAddress.fromAddressDetail,

          loadingBeginTime: orderCenter.loadingBeginTime,
          loadingEndTime: orderCenter.loadingEndTime,
          shipperName: orderFromAddress.mainDriverName,
          shipperPhone: orderFromAddress.mainDriverPhone,

          goodsTypeName: goodsInfo.goodsTypeName,
          goodsWeight: goodsInfo.goodsWeight,
          goodsVolume: goodsInfo.goodsVolume,

          transportFee: orderCenter.transportFee,

          receiveName: orderToAddress.mainDriverName,
          receivePhone: orderToAddress.mainDriverPhone,
          requireUnloadingDate: comments.loadingEndTime,
          specialRequirements: comments.needs,
          requireTruckLength: typeLength.truckLength,
          requireTruckTypeName: typeLength.truckTypeName,
          orderTruckImageList,
        }
      }
    }
    console.log(formData);
    if (this.checkFormData(formData)) {
      this.setData({ showLoading: true })
      requestUtil.postRequest(requestUrls.orderCenter.saveOrderCenter, formData, (res) => {
        this.setData({ showLoading: false })
        if (!res.hasError) {
          //移除缓存
          wx.removeStorageSync('goodsInfo');
          wx.removeStorageSync('typeLength');
          wx.removeStorageSync('comments');
          wx.removeStorageSync('addOrderCenter');
          wx.showModal({
            title: '温馨提示',
            content: '操作成功',
            showCancel: false,
            success: res => {
              wx.switchTab({
                url: '/pages/homev2/index'
              })
            }
          })
        }
      })
    }
  },
  checkFormData(formData) {
    const { orderFromAddress, orderToAddress, goodsInfo, typeLength, orderCenter } = this.data;
    if (!orderFromAddress) {
      this.showErrorMessage("请选择起运地地址");
      return false;
    }
    if (!orderToAddress) {
      this.showErrorMessage("请选择起目的地址");
      return false;
    }
    if (!goodsInfo) {
      this.showErrorMessage("请选择货物信息");
      return false;
    }
    if (!typeLength) {
      this.showErrorMessage("请选择需求车辆信息");
      return false;
    }
    if (!orderCenter.loadingBeginTime) {
      this.showErrorMessage("请选择装货时间");
      return false;
    }
    if (!orderCenter.transportFee) {
      this.showErrorMessage("请填写运费");
      return false;
    }
    return true;
  },
  /**显示错误信息 */
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const addOrderCenter = wx.getStorageSync('addOrderCenter');
    if (addOrderCenter) {
      this.setData({
        publishStr: '提交'
      });
      wx.setNavigationBarTitle({
        title: '托运申请信息',
      });
    }
    const orderFromAddress = wx.getStorageSync('orderFromAddress');
    const orderToAddress = wx.getStorageSync('orderToAddress');
    const goodsInfo = wx.getStorageSync('goodsInfo');
    const typeLength = wx.getStorageSync('typeLength');
    const comments = wx.getStorageSync('comments');
    const orderCenter = wx.getStorageSync('orderCenter');
    const { goodsImgList } = comments;
    if (goodsImgList && goodsImgList.length > 0) {
      let orderTruckImageList = [];
      goodsImgList.forEach(item => {
        orderTruckImageList.push({ imageUrl: item.imageUrl, url: requestUrls.baseImagePrefixUrl + item.imageUrl })
      });
      this.setData({
        orderTruckImageList,
      })
    }
    this.setData({
      addOrderCenter,
      orderFromAddress,
      orderToAddress,
      goodsInfo,
      typeLength,
      comments,
      orderCenter,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})